<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/themes/default/easyui.css">   
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/themes/icon.css">  
<script src="<%=request.getContextPath() %>/js/jquery-1.11.2.min.js"></script> 
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.easyui.min.js"></script> 

</head>
<body>
	
 <table id="datas"></table>   

<div id="dd" class="easyui-dialog" title="用户权限管理" style="width:400px;height:200px;"   
        data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true,buttons:'#btns'">   
        
        <span id="userInfo" style="color: red;font-size: 20px"> </span>
       <div id="rowInfo"></div> 
        
       
</div> 
<!-- dialog的两个按钮 -->
 	<div id="btns">
			<!-- <a id="btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok',onclick:add">添加</a>  
			<a id="btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">取消</a> -->
			  <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'"   
        onclick="add()">添加</a>  
        	  <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'"   
        onclick="remove()">取消</a> 
		</div> 
		
		<!-- 添加用户信息 -->
	 	
		
		<div id="vv" class="easyui-dialog" title="添加用户" style="width:400px;height:200px;"   
        data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true,buttons:'#cc'">   
        	<form id="addform">
        		用户名称：<input type="text" name="username">
        		<input type="submit" value="提交">
        	</form>
		</div>
		
		<div id="cc">
			  <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'"   
        onclick="addUser()">添加</a>  
        	  <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'"   
        onclick="removeUser()">取消</a> 
		</div> 	


<script type="text/javascript">

$('#datas').datagrid({    
    url:'<%=request.getContextPath() %>/admin/getUsers',    
    columns:[[    
        {field:'uid',title:'用户ID',width:100},    
        {field:'username',title:'用户名称',width:100},  
        {field:'xx',title:'操作',width:100,formatter: function(value,row,index){
			return "<input type='button' onClick='pow("+row.uid+")' value='用户赋角色'>";
		}
},
    ]],
    fitColumns:true,
    singleSelect:true,
    toolbar: [{
		iconCls: 'icon-add',
		handler: function(){
			//添加用户信息
			$("#vv").dialog('open');
			//用户的添加
		
			
			 $('#addform').form({    
   				 url:"<%=request.getContextPath() %>/admin/addAdminUser",    
  				  onSubmit: function(){    
  					 //首先加载，返回验证
  					  return $("#addform").form('validate');    
		   		 },    
		    success:function(obj){    
		    	if(obj){
		    		//加强版弹框
		    		if(obj==1){
		    			$.messager.alert("系统提示","保存成功");
		    			//dialog的关闭
						$("#vv").dialog("close");
						//datagrid的刷新
						$("#datas").datagrid("reload");
		    		}
					
				}else{
					$.messager.alert("系统提示","保存失败");
				}    
		    }    
		});    
			
		
			
		}
	},'-',{
		iconCls: 'icon-remove',
		handler: function(){
			//选中datagrid的一行 object
			var row = $("#datas").datagrid("getSelected");
			if(row!=null){
				if(confirm("确认删除？")){
					$.ajax({
						url:"<%=request.getContextPath() %>/admin/removeAdminUser",
						type:"post",
						data:{uid:row.uid},
						success:function(obj){
							if(obj==1){
								$.messager.alert('success','删除成功');   
								$('#datas').datagrid('reload');
							}
							
						},
						dataType:"text"
					});
					
					
				}
			}else{
				$.messager.alert('Exception','请选中一行');   
			}
			

		}
	}]

    
});   

//用户赋予角色的函数执行
function pow(uid){
	//首先打开一个dialog
	$("#dd").dialog('open') 
	//显示用户信息
	$.ajax({
		url:"<%=request.getContextPath() %>/admin/getUserByUid",
		type:"post",
		data:{uid:uid},
		success:function(obj){
			//span标签赋值
			$("#userInfo").empty();
			$("#userInfo").html("当前用户："+obj.username+"<br><input type='hidden' id='userId' value='"+obj.uid+"'/>");
		},
		dataType:"json",
	});
	
	
	
	//查询所有的角色信息 
	$.ajax({
		url:"<%=request.getContextPath() %>/admin/getRows",
		type:"post",
		data:{},
		success:function(obj){
			$("#rowInfo").empty();
			for ( var i in obj) {
				$("#rowInfo").append("<input type='checkbox' name='roleId' value='"+obj[i].rid+"'/>"+obj[i].rname+"");
			}
			//角色回显
			$.ajax({
				url:"<%=request.getContextPath() %>/admin/getUser_roleListByUid",
				type:"post",
				data:{uid:uid},
				success:function(obj){
					for ( var i in obj) {
						$("[name='roleId'][value='"+obj[i].rid+"']").attr("checked",true);
					} 
				},
				dataType:"json",
			});
			
		},
		dataType:"json",
	});
	
	
	
		
<%-- 	角色赋权限的时候使用	
	$("#trees").tree({
					url:"<%=request.getContextPath() %>/power/getPower",
					idFiled:"id",
			  		textFiled:"name",
			  		parentField:"pid",
					checkbox:true,
					lines:true,
					cascadeCheck:false,
					onClick:function(node){
						if($("#trees").tree('isLeaf',node.target)){
							alert(node.id);	
						}
					}
			
				});	 --%>
	//角色赋予成功，关闭dialog 并刷新页面	
}
//添加的事件
function add(){
	//获取uid
	 var userId=$("#userId").val();
	//获取角色id的集合
	var roles="";
	var idArray=[];
	//遍历选中的复选框进行遍历
	$("[name='roleId']:checked").map(function(){
		//roles+=","+$(this).val();
		idArray.push($(this).val());
	});
	
	//alert(roles.substring(1));
	roles=idArray.join();
//添加用户角色信息
	$.ajax({
		url:"<%=request.getContextPath() %>/admin/addUser_role",
		type:"post",
		data:{uid:userId,rids:roles},
		success:function(obj){
			if(obj==1){
				$("#dd").dialog('close');
				$("#datas").datagrid('reload');
			}
			
		},
		dataType:"text",
		
		
		
	});
	
	
	//在添加前先回显树的选中状态
	
	//选中用户的id    在选中
	
	
	
	
	
	/* 用户赋角色        角色赋权限的时候使用
		var nodes=$("#trees").tree('getChecked');
		var ids="";
		var idArray=[];
		for ( var i in nodes) {
			idArray.push(nodes[i].id);
		}
		ids=idArray.join(); */
		//alert(ids);	
}
		
</script> 

</body>
</html>